<?php

namespace app\common\model;

use app\common\model\base\BaseModel;
use app\common\traits\InstanceTrait;
use think\Model;
use think\model\Collection;
use think\model\Pivot;


/**
 * Class Member
 * @package app\api\model
 */
class Member extends BaseModel
{
    use InstanceTrait;

    public function __construct($data = [], Model $parent = NULL, $table = '')
    {
        parent::__construct($data, $parent, $table);
        $this->pk = 'member_id';
        $this->initBaseField();
    }

    /**
     * 关联一对多-分类
     * @return \think\model\relation\HasMany
     */
    public function categorys()
    {
        return $this->hasMany(Category::class, 'creater_id', $this->pk)
                    ->where('status', '=', 1)
                    ->alias('category')
                    ->field(['category.*', 'category.category_id as id']);
    }

    /**
     * 关联一对多-获取用户所属所有用户组
     * @return \think\model\relation\HasMany
     */
    public function groups()
    {
        return $this->belongsToMany(MemberGroup::class, MemberGroupRelation::class, 'group_id', $this->pk)
                    ->where('status', '=', 1)
                    ->cache(TRUE, 60)
                    ->alias('group')
                    ->field(['group.*', 'group.group_id as id'])
                    ->hidden($this->pivotHiddenField);
    }

    /**
     * @param array $param
     * @param string $alias
     * @return array|mixed
     */
    public function getBaseWhere($param = [], $alias = '')
    {
        if (!empty($alias)) {
            $alias = "{$alias}.";
        }
        $where = parent::getBaseWhere($param, $alias);
        if (!empty($param['keywords'])) {
            $where[] = [$alias . 'username|realname', 'like', '%' . $param['keywords'] . '%'];
        }
        if (!empty($param['group_id'])) {
            $group = MemberGroup::instance()->getByID($param['group_id']);
            $admin_id_arr = $group->members->column('admin_id');
            $where[] = [$alias . 'admin_id', 'in', $admin_id_arr];
        }
        return $where;
    }

    public function getByPhone($phone)
    {
        return $this->where('phone', $phone)->find();
    }

    public function getByOpenid($openid)
    {
        return $this->where('openid', $openid)->find();
    }

    public function getByName($name)
    {
        $where = [
            ['member_name|phone', '=', $name],
        ];
        $ret = static::where($where)
                     ->field($this->baseField)
                     ->find();
        return $ret;
    }

    /**
     * 获取列表
     * @return mixed|Model|Pivot|null
     * @throws DbException
     */
    public function getList($param)
    {
        $where = $this->getBaseWhere($param);
        $param['page'] = !empty($param['page']) ? $param['page'] : 1;
        $param['limit'] = !empty($param['limit']) ? $param['limit'] : 15;
        $data = static::where($where)
                      ->field($this->baseField)
                      ->page($param['page'], $param['limit'])
                      ->select();
        return $data;
    }

    /**
     * 获取admin的rule数组
     * @param $admin_id
     * @return \think\model\Collection|null
     * @throws DbException
     */
    public function getRuleList($admin_info)
    {
        $rule_arr = new Collection();
        if (!empty($admin_info)) {
            $groups = $admin_info->groups;
            foreach ($groups as $k => $v) {
                $rule_arr = $v['rules']->merge($rule_arr);
            }
        }
        return \think\model\Collection::make($rule_arr);
    }
}
